﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RMS.Common;
using RMS.DB;

namespace RMS
{
    public partial class frmListStock : ICommonForm
    {
        public frmListStock()
        {
            InitializeComponent();
        }
        private InventoryMng inventoryMng = new InventoryMng();
        private ConfigurationMng _configurationMng = new ConfigurationMng();
        private SupplierMng _supplierMng = new SupplierMng();
        private void frmListStock_Load(object sender, EventArgs e)
        {
            BindAllInventory();
            BindDataCmbBox();
        }
        private void BindAllInventory()
        {
            var src = inventoryMng.GetAllInventory();
            if (src.Count == 0)
            {
                btnSelectAll.Enabled = btnUnselect.Enabled = btnDel.Enabled = false;
            }
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = src;
            dataGridView1.Refresh();
        }

        private void BindDataCmbBox()
        {
            var listCategory = _configurationMng.GetAllItems(Constants.CategoryType);
            var listSupply = _supplierMng.GetAllItems();
            foreach (var item in listCategory)
            {
                if (!string.IsNullOrEmpty(item.Name))
                {
                    cmbCategory.Items.Add(item.Name);
                }
            }
            foreach (var item in listSupply)
            {
                cmbSupply.Items.Add(item.ID);
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = inventoryMng.GetInventoryByElements(txtCode.Text.Trim(), cmbCategory.Text.Trim(), cmbSupply.Text.Trim());
        }

        private void btnSelectAll_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = true;
            }
        }

        private void btnUnselect_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = false;
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            var flag = dataGridView1.Rows.Cast<DataGridViewRow>().Count(row => Convert.ToBoolean(row.Cells[0].Value));
            if (flag == 0)
            {
                MessageBox.Show(Constants.Msg.ChooseOneItem, "Chú ý");
                return;
            }

            if (MessageBox.Show(Constants.Msg.DeleteItem, "Chú ý", MessageBoxButtons.YesNo) != DialogResult.Yes)
                return;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                if (Convert.ToBoolean(row.Cells[0].Value))
                    inventoryMng.DeleteItem(Convert.ToString(row.Cells[1].Value));
            }
            BindAllInventory();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 1)
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (row.Cells[1].Selected)
                    {
                        row.Selected = true;
                        var id = Convert.ToString(row.Cells[1].Value);
                        if (!string.IsNullOrEmpty(id))
                        {
                            var frmInv = new frmInventory(id) { MdiParent = frmMain.ActiveForm };
                            frmInv.Show();
                            this.Close();
                        }
                        return;
                    }
                }
            }
        }

        private void btnshowAll_Click(object sender, EventArgs e)
        {
            BindAllInventory();
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            var frm = new frmInventory() { MdiParent = frmMain.ActiveForm, WindowState = FormWindowState.Maximized };
            frm.Show();
            this.Close();
        }
    }
}
